import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:http/http.dart' as http;

void main() {
  client4();
}

void client() async {
  var webSocket = await WebSocket.connect('ws://127.0.0.1:1234/ws');
  webSocket.add('hello world from websocket');
  webSocket.listen((event) {
    print('${event}');
  });
}


void client4() async {
  var url = Uri.parse('https://10.0.0.10:19999/api/info');
  var response = await http.get(url);
  print('Response status: ${response.statusCode}');
  print('Response body: ${response.body}');
}

void client3() async {
  var ctx = SecurityContext(withTrustedRoots: false);
  ctx.useCertificateChainBytes(utf8.encode(CERTIFICATE));
  ctx.usePrivateKeyBytes(utf8.encode(PRIVATE_KEY));
  var socket = await SecureSocket.connect(
    "10.0.0.10",
    20000,
    context: ctx,
    onBadCertificate: (X509Certificate) => true,
  ).catchError((e) {
    print('$e');
  });
  // var webSocket = WebSocket.fromUpgradedSocket(socket, serverSide: false);
  // var webSocket = await WebSocket.connect('wss://10.0.0.10:19999/wss/bridging');
  var byteData = ByteData(5);
  byteData.setInt32(0, 0);
  byteData.setUint8(4, 250);
  socket.add(byteData.buffer.asUint8List());
  // webSocket.listen((event) {
  //   print('${event}');
  // });
}

void client2() async {
  var webSocket = await WebSocket.connect('wss://10.0.0.10:19999/wss/bridging');
  var byteData = ByteData(5);
  byteData.setInt32(0, 0);
  byteData.setUint8(4, 250);
  webSocket.add(byteData.buffer.asUint8List());
  webSocket.listen((event) {
    print('${event}');
  });
  sleep(Duration(seconds: 60));
}

void server() async {
  try {
    var server = await HttpServer.bind('127.0.0.1', 1234);
    await for (HttpRequest request in server) {
      if (request.uri.path == '/ws') {
        var webSocket = await WebSocketTransformer.upgrade(request);
        // server.asBroadcastStream(
        //     onListen: (StreamSubscription streamSubscription) {
        //   streamSubscription.onData((data) {
        //     print("received from client: ${data}");
        //     webSocket.add('success');
        //   });
        // });
        /*webSocket.asBroadcastStream(
        */ /*    onListen: (StreamSubscription streamSubscription) {
          streamSubscription.onData((data) {
            print("received from client: ${data}");
            webSocket.add('success');
          });
        }*/ /*).listen((event) {
          print('event-> $event');
            webSocket.add('ok');
        });*/
        webSocket.listen((message) {
          print('received message: ${message}');
          webSocket.add('ok: ${message}');
        });
      }
    }
  } catch (e) {
    print(e);
  }
}

const CERTIFICATE = '''-----BEGIN CERTIFICATE-----
MIIDGzCCAgMCCQCkDB0MUU64dDANBgkqhkiG9w0BAQsFADCBkjELMAkGA1UEBhMC
Q04xETAPBgNVBAgMCFNoYW5nSGFpMREwDwYDVQQHDAhTaGFuZ0hhaTEVMBMGA1UE
CgwMRnVydGhlclNlbnNlMQswCQYDVQQLDAJGUzEVMBMGA1UEAwwMRnVydGhlclNl
bnNlMSIwIAYJKoZIhvcNAQkBFhNjaGVueWFseWdAZ21haWwuY29tMB4XDTIwMDkw
MzE2NTYzMVoXDTMwMDkwMTE2NTYzMVowDDEKMAgGA1UEAwwBKjCCASIwDQYJKoZI
hvcNAQEBBQADggEPADCCAQoCggEBALBlzlU1cjGvDzc/+ll7bZzwPB2vX+Zsasir
bfYx31HoYrchFNFGb/P9vscfW8gXGGna4XEgIcLoRv4ZzYT1h6Ts0Z5DaROB84iD
KikYQ4XZYPKzTSTHkJS2TYWtd1qZvv7C6eoxW+juKolDpJE/AsAc9YDpWjd14bnH
QNTzsmpFw2sHKjSvCyPuea1XSg7Rhu2hFCY8jAoDZ+aNeZfvCUdjUpLDRqL3Z+GC
4X/zd/gmmY7mKsP6QgjggTCZ7mTIclCTBYGttV29p4eNLGKy0Mj3yDof6k1SdxRo
WgqfIlu90tzkNGLlnHsFNTdrXQ0zPtjtN0dADk9s3QJEX8UozcMCAwEAATANBgkq
hkiG9w0BAQsFAAOCAQEARJBKfR+PgCG8VeqFgVtgw9wSvvlh9K1xR5YgflUSTmZ+
pTlOfP6BgDfspb7yF4+YrrlbTk3Jo4R8KsOtXHtdFOA8sopPZ1dL2SPj6NOqahH7
XoH2LhmvZGqJyj9SRuZRgnL7T8W7CSdwvLeiJzA+jP6hM8bY2jneUoBWzrFMO8vP
cNTOqJeMC7y5HSig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